package com.pkx.cloud.fse7.dms.realtimestate.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.pkx.cloud.fse7.conf.StatusEnum;
import com.pkx.cloud.fse7.dms.monitoring.service.StatusService;
import com.pkx.cloud.fse7.dms.realtimestate.entity.RealTimeState;
import com.pkx.cloud.fse7.dms.realtimestate.repo.RealTimeStateMapper;
import com.pkx.cloud.fse7.dms.realtimestate.service.RealTimeStateService;
import com.pkx.cloud.fse7.dms.site.entity.DoorDeviceSite;
import com.pkx.cloud.fse7.dms.site.service.DoorDeviceSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author ly
 * @since 2020-10-14
 */
@Service
@DS("master")
public class RealTimeStateServiceImpl extends ServiceImpl<RealTimeStateMapper, RealTimeState> implements RealTimeStateService {

    @Resource
    private RealTimeStateMapper realTimeStateMapper;
    @Resource
    private DoorDeviceSiteService siteService;

    @Autowired
    private StatusService statusService;

    /**
     * 分页列表
     *
     * @param realTimeState
     * @return
     */
    @Override
    public Page<RealTimeState> pageList(Page page, RealTimeState realTimeState) {
        LambdaQueryWrapper<RealTimeState> lambdaQueryWrapper = Wrappers.lambdaQuery(RealTimeState.class);
        Page page1 = page(page, lambdaQueryWrapper);

        return page(page, lambdaQueryWrapper);
    }

    /**
     * 列表查询
     *
     * @param realTimeState
     * @return
     */
    @Override
    public List<RealTimeState> listByEntity(RealTimeState realTimeState) {
        LambdaQueryWrapper<RealTimeState> lambdaQueryWrapper = Wrappers.lambdaQuery(RealTimeState.class);
        return list(lambdaQueryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public RealTimeState selectById(Long id) {
        return getById(id);
    }

    @Override
    public Page queryPage(Page page, String areaId) {
        // 查询所有子区域
        Set<String> strings = realTimeStateMapper.quertAreas(areaId);
        // 将自己id 添加进去
        strings.add(areaId);
        Page<Map<String, Object>> listPage = realTimeStateMapper.queryPage(page, strings);
        Map<String, Integer> status = statusService.getStatus();
        listPage.getRecords().stream().forEach(item -> {
            String devNo = (String) item.get("Rid");
            String doorNo = String.valueOf(item.get("DoorNo"));
            if (status.containsKey(devNo.concat(":").concat(doorNo))) {
                item.put("st", status.get(devNo.concat(":").concat(doorNo)));
            } else {
                item.put("st", StatusEnum.normal.getCode());
            }
        });

        return listPage;
    }

    @Override
    public List<Map<String, Object>> queryList(String areaId) {
        // 查询所有子区域
        Set<String> strings = realTimeStateMapper.quertAreas(areaId);
        // 将自己id 添加进去
        strings.add(areaId);
        List<Map<String, Object>> maps = realTimeStateMapper.queryPage(strings);
        if (maps.isEmpty()) {
            return maps;
        }
        // 筛选出读头id,用于查询对应的定位
        Set<String> readIds = maps
                .stream()
                .map(map -> (String) map.get("id"))
                .collect(Collectors.toSet());
        // 查询定位信息
        List<DoorDeviceSite> doorDeviceSites = siteService.listByIds(readIds);
        Map<String, String> sitMaps = getMaps(doorDeviceSites);
        // 加上定位信息
        maps.stream().forEach(map -> {
            String id = (String) map.get("id");
            map.put("siteX", Strings.nullToEmpty(sitMaps.get("siteX" + id)));
            map.put("siteY", Strings.nullToEmpty(sitMaps.get("siteY" + id)));
            map.put("translate", Strings.nullToEmpty(sitMaps.get("translate" + id)));
        });
        return maps;
    }

    public Map<String, String> getMaps(List<DoorDeviceSite> list) {
        HashMap<String, String> maps = Maps.newHashMap();
        list.stream().forEach(doorDeviceSite -> {
            maps.put("siteX" + doorDeviceSite.getId(), doorDeviceSite.getSiteX());
            maps.put("siteY" + doorDeviceSite.getId(), doorDeviceSite.getSiteY());
            maps.put("translate" + doorDeviceSite.getId(), doorDeviceSite.getTranslate());
        });
        return maps;
    }

    @Override
    public Page<List<Map<String, Object>>> deviceList(Page page, Set<String> ridList) {
        return baseMapper.deviceList(page, ridList);
    }

    @Override
    public Map statistical(String areaId) {
        // 查询所有子区域
        Set<String> strings = realTimeStateMapper.quertAreas(areaId);
        // 将自己id 添加进去
        strings.add(areaId);
        return realTimeStateMapper.statistical(strings);

    }

    @Override
    public Page<List<Map<String, Object>>> offline(Page page, String areaId) {
        // 查询所有子区域
        Set<String> strings = realTimeStateMapper.quertAreas(areaId);
        // 将自己id 添加进去
        strings.add(areaId);

        return realTimeStateMapper.offline(page, strings);
    }

    @Override
    public List<Map<String, Object>> readList(String areaId) {
        // 查询所有子区域
        Set<String> strings = realTimeStateMapper.quertAreas(areaId);
        // 将自己id 添加进去
        strings.add(areaId);
        List<Map<String, Object>> maps = realTimeStateMapper.readList(strings);
        return maps;
    }

    @Override
    public Page<List<Map<String, Object>>> grantPage(Page page, Integer devNo, Integer doorNo) {
        return realTimeStateMapper.grantPage(page, devNo, doorNo);
    }

    @Override
    public List<Map<String, Object>> grantList(Integer devNo, Integer doorNo) {
        return realTimeStateMapper.grantPage(devNo, doorNo);
    }
}
